package net.maku.kyy.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.kyy.convert.KyyUserRoleConvert;
import net.maku.kyy.entity.KyyUserRoleEntity;
import net.maku.kyy.query.KyyUserRoleQuery;
import net.maku.kyy.service.KyyUserRoleService;
import net.maku.kyy.vo.KyyUserRoleVO;
import net.maku.kyy.dao.KyyUserRoleDao;
import com.fhs.trans.service.impl.TransService;
import net.maku.framework.common.utils.ExcelUtils;
import net.maku.kyy.vo.KyyUserRoleExcelVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户角色关系
 *
 * @author MAKU babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@Service
@AllArgsConstructor
public class KyyUserRoleServiceImpl extends BaseServiceImpl<KyyUserRoleDao, KyyUserRoleEntity> implements KyyUserRoleService {
    private final TransService transService;

    @Override
    public PageResult<KyyUserRoleVO> page(KyyUserRoleQuery query) {
        IPage<KyyUserRoleEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(KyyUserRoleConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }


    private LambdaQueryWrapper<KyyUserRoleEntity> getWrapper(KyyUserRoleQuery query){
        LambdaQueryWrapper<KyyUserRoleEntity> wrapper = Wrappers.lambdaQuery();

        return wrapper;
    }


    @Override
    public KyyUserRoleVO get(Long id) {
        KyyUserRoleEntity entity = baseMapper.selectById(id);
        KyyUserRoleVO vo = KyyUserRoleConvert.INSTANCE.convert(entity);

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(KyyUserRoleVO vo) {
        KyyUserRoleEntity entity = KyyUserRoleConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(KyyUserRoleVO vo) {
        KyyUserRoleEntity entity = KyyUserRoleConvert.INSTANCE.convert(vo);

        updateById(entity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);

    }



    @Override
    public void export() {
    List<KyyUserRoleExcelVO> excelList = KyyUserRoleConvert.INSTANCE.convertExcelList(list());
        transService.transBatch(excelList);
        ExcelUtils.excelExport(KyyUserRoleExcelVO.class, "用户角色关系", null, excelList);
    }

}